package giit.computerbase.server;

import giit.computerbase.dao.SQL;
import giit.computerbase.dao.SQLFactory;
import giit.computerbase.util.Application;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * User: tum0r
 * Package Name: giit.computerbase.server
 * Time: 2018/11/11 Sunday 08:22:11
 */
public class InitializeServer {
    private SQL sql;

    public InitializeServer() {
        try {
            sql = SQLFactory.getSQL();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String init(String ua) {
        sql.open((String) Application.getConfig().databaseConfig.get("address"),
                (int) Application.getConfig().databaseConfig.get("port"),
                "",
                (String) Application.getConfig().databaseConfig.get("encode"),
                (String) Application.getConfig().databaseConfig.get("user"),
                (String) Application.getConfig().databaseConfig.get("password"));
        sql.update("create database if not exists " + Application.DATABASE_NAME, null);
        try {
            sql.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        sql.open((String) Application.getConfig().databaseConfig.get("address"),
                (int) Application.getConfig().databaseConfig.get("port"),
                Application.DATABASE_NAME,
                (String) Application.getConfig().databaseConfig.get("encode"),
                (String) Application.getConfig().databaseConfig.get("user"),
                (String) Application.getConfig().databaseConfig.get("password"));
        for (int i = 0; i < Application.CREATE_TABLE.length; i++) {
            sql.update(Application.CREATE_TABLE[i], null);
        }
        ArrayList<HashMap<String, Object>> temp = sql.query("select * from user", null);
        boolean flag = false;
        if (temp.size() == 0) {
            sql.add("insert into organization values(?,?)", Application.COMPUTER_BASE_NAME);
            for (int i = 0; i < Application.USER_TYPES.length; i++) {
                sql.add("insert into user_type values(?,?)", Application.USER_TYPES[i]);
            }
            for (int i = 0; i < Application.ACTIVITY_TYPES.length; i++) {
                sql.add("insert into activity_type values(?,?)", Application.ACTIVITY_TYPES[i]);
            }
            Application.INITIALIZE_PASSWORD = getPassword();
            UserServer userServer = new UserServer();
            flag = userServer.register("tum0r", Application.COMPUTER_BASE_NAME[1], Application.INITIALIZE_PASSWORD);
            flag = sql.alter("update user set user_type_id=?", new Object[]{Application.USER_TYPES[1][0]});
        }
        try {
            sql.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        StringBuffer result = new StringBuffer();
        result.append("{\"state\":" + flag);
        if (Application.INITIALIZE_PASSWORD != null) {
            result.append(",\"password\":\"" + Application.INITIALIZE_PASSWORD + "\"");
        }
        result.append("}");
        Application.INITIALIZE_PASSWORD = null;

        HistoryServer historyServer = new HistoryServer();
        historyServer.record("127.0.0.1", ua, Application.COMPUTER_BASE_NAME[1], "系统管理员", "root", "初始化数据库", flag ? "成功" : "失败");

        return result.toString();
    }

    private String getPassword() {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        int length = (int) Application.getConfig().databaseConfig.get("initialize_password_length");
        int n;
        char c = '0';
        for (int i = 0; i < length; i++) {
            n = random.nextInt(3);
            switch (n) {
                case 0:
                    c = (char) ('0' + random.nextInt(10));
                    break;
                case 1:
                    c = (char) ('a' + random.nextInt(26));
                    break;
                case 2:
                    c = (char) ('A' + random.nextInt(26));
                    break;
            }
            result.append(c);
        }
        return result.toString();
    }
}
